﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Einstein.Data;
using System.Data;

namespace Einstein.Group.DAL
{
    public class DictionaryDAL
    {
        public List<Dictionary> SelectAll()
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.Dictionary.ToList<Dictionary>();
            }
        }

        public List<Dictionary> GetByCategory(string category)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.Dictionary.Where(c => c.Category == category).ToList();
            }
        }

        public Dictionary GetByCategoryAndKeyName(string category, string keyName)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.Dictionary.Where(c => c.Category == category && c.KeyName == keyName).FirstOrDefault();
            }
        }

        public Dictionary GetByCategoryAndKeyValue(string category, int keyValue)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.Dictionary.Where(c => c.Category == category && c.KeyValue == keyValue).FirstOrDefault();
            }
        }

        public bool ExistCategory(Dictionary dic)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.Dictionary.Count(c => c.Category == dic.Category) > 0;
            }
        }

        public bool ExistKeyName(Dictionary dic, bool isEdit)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                if (isEdit)
                {
                    return entities.Dictionary.Count(c => c.Category == dic.Category && c.KeyName == dic.KeyName && c.ID != dic.ID) > 0;
                }
                else
                {

                    return entities.Dictionary.Count(c => c.Category == dic.Category && c.KeyName == dic.KeyName) > 0;
                }

            }
        }


        public bool Update(Dictionary info)
        {

            using (EinsteinEntities entities = new EinsteinEntities())
            {
                Dictionary item = entities.Dictionary.FirstOrDefault(a => a.ID == info.ID);
                if (item != null)
                {
                    item.Category = info.Category;
                    item.KeyName = info.KeyName;
                    item.KeyValue = info.KeyValue;
                }
                return entities.SaveChanges() > 0;
            }
        }
        public bool Delete(Dictionary info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                Dictionary item = entities.Dictionary.FirstOrDefault(a => a.ID == info.ID);
                if (item != null)
                {
                    entities.DeleteObject(item);
                }
                return entities.SaveChanges() > 0;
            }
        }

        public bool DeteteByCategory(Dictionary info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                List<Dictionary> list = entities.Dictionary.Where(a => a.Category == info.Category).ToList();
                if (list != null)
                {
                    foreach (Dictionary item in list)
                    {
                        entities.Dictionary.DeleteObject(item);
                    }
                }
                return entities.SaveChanges() > 0;
            }
        }

        public bool Add(Dictionary info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                entities.Dictionary.AddObject(info);
                return entities.SaveChanges() > 0;
            }
        }

        public bool UpdateCategory(string newcategory, string oldcategory)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                List<Dictionary> list = entities.Dictionary.Where(a => a.Category == oldcategory).ToList();
                if (list != null)
                {
                    foreach (Dictionary item in list)
                    {
                        item.Category = newcategory;
                    }
                }
                return entities.SaveChanges() > 0;
            }
        }
    }
}
